<template>
  <view class="cart">
    <!-- 1 列表 -->
    <view
      class="cart-list"
      v-for="(item, index) in cartList"
      :key="item.goods_id"
    >
      <view class="cart-item">
        <!-- 1 复选框 -->
        <view class="goods-chk">
          <u-checkbox
            :value="item.checked"
            shape="circle"
            active-color="red"
            @change="checkedGoods(index)"
          ></u-checkbox>
        </view>
        <!-- 2 图片 -->
        <view class="goods-img">
          <u-image
            width="191rpx"
            height="191rpx"
            :src="item.goods_small_logo"
          ></u-image>
        </view>
        <!-- 3 信息 -->
        <view class="goods-info">
          <!-- 名称 -->
          <view class="goods-name u-line-2">{{ item.goods_name }}</view>
          <!-- 价格和数量 -->
          <view class="goods-price-num">
            <view class="goods-price">￥{{ item.goods_price }}</view>
            <view class="goods-num-tool">
              <view class="num-btn" @click="btnChange(-1, index)">-</view>
              <view class="goods-vnum">{{ item.nums }}</view>
              <view class="num-btn" @click="btnChange(1, index)">+</view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 2 统计 -->
    <view class="statistics">
      <view class="all-chk">
        <u-checkbox
          :value="allChecked"
          shape="circle"
          active-color="red"
          @change="allGoodsChecked"
          >全选</u-checkbox
        >
      </view>
      <view class="all-price">
        合计: <text>￥{{ totalCount }}</text>
      </view>
      <view class="all-count">
        <u-button type="error" shape="circle" @click="cartGoPay"
          >去结算({{ totalNums }})</u-button
        >
      </view>
    </view>
  </view>
</template>

<script>
import { mapState, mapMutations, mapGetters } from "vuex";
export default {
  computed: {
    ...mapState({
      cartList: (store) => store.cart.cartList,
    }),
    ...mapGetters("cart", ["totalCount", "totalNums"]),
    // 小选控制全选
    allChecked() {
      return (
        this.cartList.length && this.cartList.every((item) => item.checked)
      );
    },
    // 合计
    // totalCount() {
    //   let count = 0;
    //   this.cartList.forEach((item) => {
    //     if (item.checked) {
    //       count += item.goods_price * item.nums;
    //     }
    //   });
    //   return count;
    // },
    // 结算数量
    // totalNums() {
    //   let nums = 0;
    //   this.cartList.forEach((item) => {
    //     if (item.checked) {
    //       nums += item.nums;
    //     }
    //   });
    //   return nums;
    // },
  },
  methods: {
    ...mapMutations([
      "cart/cartGoodsChecked",
      "cart/cartGoodsCount",
      "cart/cartGoodsRemove",
      "cart/cartAllGoodsChecked",
    ]),
    // 跳转到支付页面
    cartGoPay() {
      // 判断当前跳转按钮是否有值
      if (this.totalNums === 0) {
        uni.showToast({
          title: "当前选中的商品为空",
          icon: "none",
        });
      } else {
        // console.log("支付页面");
        // 可以使用uni.navigateTo,保留当前页面，跳转到其他页面
        uni.navigateTo({ url: "/pages/pay/pay" });
      }
    },
    // 点击全选控制所有小选
    allGoodsChecked() {
      const allChecked = !this.allChecked;
      this["cart/cartAllGoodsChecked"]({ checked: allChecked });
    },
    // 修改商品单个复选框
    checkedGoods(index) {
      // console.log(index);
      this["cart/cartGoodsChecked"]({ index });
    },
    // 修改商品数量
    async btnChange(unit, index) {
      // console.log(unit, index);
      if (this.cartList[index].nums === 1 && unit === -1) {
        const [err, res] = await uni.showModal({
          title: "提示",
          content: "您是否想要删除",
          showCancel: true,
          cancelText: "取消",
          cancelColor: "#000000",
          confirmText: "确定",
          confirmColor: "#3CC51F",
        });
        if (res.confirm) {
          // del
          this["cart/cartGoodsRemove"]({ index });
          return;
        }
      } else {
        this["cart/cartGoodsCount"]({ unit, index });
      }
    },
  },
};
</script>

<style lang="scss">
.cart {
  padding-bottom: 111rpx;
  .cart-list {
    .cart-item {
      display: flex;
      padding: 10rpx;
      border-bottom: 1rpx solid #ccc;
      .goods-chk {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .goods-img {
        flex: 2;

        display: flex;
        align-items: center;
        justify-content: center;
      }

      .goods-info {
        color: #666;
        flex: 3;
        display: flex;
        flex-direction: column;
        justify-content: space-around;

        .goods-price-num {
          display: flex;
          justify-content: space-between;
          .goods-price {
            color: $themeColor;
          }

          .goods-num-tool {
            width: 140rpx;
            height: 40rpx;

            display: flex;
            justify-content: space-between;
            .num-btn {
              border: 1rpx solid #ccc;
              border-radius: 50%;
              width: 40rpx;
              height: 40rpx;
              text-align: center;
              line-height: 40rpx;
            }
          }
        }
      }
    }
  }
  .statistics {
    position: fixed;
    background-color: #fff;
    bottom: 0;
    left: 0;
    width: 100%;
    z-index: 100;
    border-top: 1rpx solid #ccc;
    display: flex;
    align-items: center;
    padding: 15rpx;

    .all-price {
      flex: 1;
      color: #666;
    }
  }
}
</style>